package com.gw.smscheck.controller.sys;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;

import com.gw.smscheck.util.ResultObj;
import com.gw.smscheck.util.ResultConversion;
import io.swagger.annotations.Api;
import springfox.documentation.annotations.ApiIgnore;
import io.swagger.annotations.ApiOperation;
import com.gw.smscheck.entity.sys.SysRole;
import com.gw.smscheck.service.sys.SysRoleService;
/**
 * SysRole管理
 * 
 * @author lhq
 * @date 2018年11月15日
 */
@Api(description="【角色模块名称】")
@Controller
@RequestMapping("/sysRoleController")
public class SysRoleController {
	@Autowired
	private SysRoleService sysRoleService;
	/**
	 * 跳转到查询页面
	 * 
	 * @return
	 */
	@RequestMapping("/goPage")
	@ApiIgnore
	public String goPage() {
		return "business/views/url/sysrole";
	}
	/**
	 * 跳转到编辑页面
	 * 
	 * @return
	 */
	@RequestMapping("/goEdit")
	@ApiIgnore
	public ModelAndView goEdit( SysRole sysRole) {
		String id = "";
		if (sysRole.getId() != null){
			id = sysRole.getId().toString();
		}
		ModelAndView modelAndView = new ModelAndView("business/views/url/sysroleedit");
		modelAndView.addObject("id", id);
		return modelAndView;
	}
	/**
	 * 添加
	 * @param response
	 * @param sysRole
	 * @return
	 */
	@RequestMapping(value = "/add", method = { RequestMethod.POST})
	@ResponseBody
	@ApiOperation(value="添加", notes="添加")
	public String add(HttpServletRequest request,HttpServletResponse response, SysRole sysRole,HttpServletRequest req) {
		HttpSession session = req.getSession();
		String userName = String.valueOf(session.getAttribute("userName"));
		sysRole.setCreateUser(userName);
		//User user = (User) request.getSession().getAttribute("user");
		//sysRole.setCreateUser(String.valueOf(user.getId()));
		return ResultObj.successResultObj(sysRoleService.add(sysRole));
	}
	/**
	 * 查询所有
	 * 
	 * @return
	 */
	@RequestMapping(value = "/findAll", method = { RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	@ApiOperation(value="查询所有", notes="查询所有")
	public String findAll() {
		List<SysRole> list = sysRoleService.findAll();
		return JSON.toJSONString(list);
	}
	/**
	 * 分页查询
	 * @param sysRole
	 * @return
	 */
	@RequestMapping(value = "/pageSelect", method = { RequestMethod.POST })
	@ResponseBody
	@ApiOperation(value="分页查询", notes="分页查询")
	public String pageSelect(SysRole sysRole) {
		PageInfo<SysRole> pageInfo = sysRoleService.pageSelect(sysRole);
		return ResultConversion.ToEasyUIDataGrid(pageInfo).toJSONString();
	}
	/**
	 * 通过id获取
	 * @param dto
	 * @return
	 */
	@RequestMapping(value = "/findById", method = { RequestMethod.POST})
	@ResponseBody
	@ApiOperation(value="通过id获取", notes="通过id获取")
	public String findById(SysRole dto){
		return ResultObj.successResultObj(sysRoleService.findById(dto.getId()));
	}
	/**
	 * 修改
	 * @param sysRole
	 * @return
	 */
	@RequestMapping(value = "/update", method = { RequestMethod.POST})
	@ResponseBody
	@ApiOperation(value="修改", notes="修改")
	public String update(SysRole sysRole) {
		return ResultObj.successResultObj(sysRoleService.update(sysRole));
	}
	/**
	 * 删除
	 * @param sysRole
	 * @return
	 */
	@ApiOperation(value="删除", notes="删除")
	@RequestMapping(value="/delById",method={RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public String del(SysRole sysRole){
		sysRoleService.delById(sysRole);
		return ResultObj.successResultObj("删除成功！");
	}
}

